package cn.com.twoke.application.game.mario.core.ecs.component.render.animation;


import cn.com.twoke.application.game.mario.core.ecs.component.ColliderComponent;
import cn.com.twoke.application.game.mario.core.ecs.component.render.RenderComponentHandler;
import cn.com.twoke.application.game.mario.core.ecs.entity.Entity;

import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * 动画渲染
 */
public class AnimationRenderComponentHandler implements RenderComponentHandler<AnimationRenderComponent> {

    @Override
    public void handle(Entity entity, BufferedImage container, Graphics2D g, AnimationRenderComponent component) {
        if (component.getAnimationTick() >= component.getAnimationTickCount()) {
            component.setAnimationTick(0);
        }
        BufferedImage animationTexture = component.getAnimationTickTexture();
        ColliderComponent colliderComponent = entity.getComponent(ColliderComponent.class);
        Shape shape = colliderComponent.shape.get();
        g.drawImage(animationTexture, shape.getBounds().x, shape.getBounds().y, shape.getBounds().width, shape.getBounds().height, null);
        if (component.getAnimationCostTime() >= component.getAnimationTickMaxTime()) {
            component.incAnimationTick();
            component.setAnimationCostTime(0);
        }
        component.incAnimationCostTime();
    }
}
